package com.feicheng.service.a;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feicheng.common.exception.SystemException;
import com.feicheng.common.util.StringGenerator;
import com.feicheng.persistence.entity.*;
import com.feicheng.persistence.mapper.AcommissionitemMapper;
import com.feicheng.persistence.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class FZjService {

    @Autowired
    IAstoreService astoreService;

    @Autowired
    IAzjService zjService;

    @Autowired
    IAzjgsService zjgsService;

    @Autowired
    IAzjsqitemService zjsqitemService;

    @Autowired
    IAcommissionService commissionService;

    @Autowired
    IAcommissionitemService commissionitemService;

    @Autowired
    IAroomyyjlService roomyyjlService;

    @Autowired
    IAjiaolianService ajiaolianService;

    @Autowired
    AcommissionitemMapper commissionitemMapper;

    /**
     * 中介公司保存
     * @param param
     * @return
     */
    @Transactional
    public JSONObject saveZjgs(JSONObject param) {
        Azjgs azjgs = param.getObject("azjgs", Azjgs.class);

        QueryWrapper<Azjgs> zjgsqw = new QueryWrapper<>();
        zjgsqw.like("zjgsnc", azjgs.getZjgsnc());
        if (StringUtils.hasLength(azjgs.getZjgsid())) {
            zjgsqw.ne("zjgsid", azjgs.getZjgsid());
        }

        synchronized (azjgs.getZjgsnc().intern()) {
            Long count = zjgsService.count(zjgsqw);
            if (count > 0) {
                throw new SystemException("公司名称重复");
            }

            if (StringUtils.hasLength(azjgs.getZjgsid())) {
                zjgsService.updateById(azjgs);
            } else {
                azjgs.setZjgsid(StringGenerator.getID("ZJGS"));
                zjgsService.save(azjgs);
                Azj azj = zjService.getById(azjgs.getFzzjid());

                if (StringUtils.hasLength(azj.getZjgsid())) {
                    throw new SystemException("当前业务员已经存在中介公司");
                }

                azj.setZjgsid(azjgs.getZjgsid());
                zjService.updateById(azj);
                param.put("azj", azj);
            }
        }
        param.put("azjgs", azjgs);
        return param;
    }

    /**
     * 取得所有中介公司（按条件）
     * @param param
     * @return
     */
    public JSONObject getZjgsAll(JSONObject param) {

        String zjgsnc = param.getString("zjgsnc");
        QueryWrapper<Azjgs> zjgsqw = new QueryWrapper<>();

        if (StringUtils.hasLength(zjgsnc)) {
            zjgsqw.like("zjgsnc", zjgsnc);
        }

        List<Azjgs> azjgses = zjgsService.list(zjgsqw);
        param.put("azjgses", azjgses);
        return param;
    }

    /**
     * 取得中介公司详细
     * @param param
     * @return
     */
    public JSONObject getZjgs(JSONObject param) {

        String zjgsid = param.getString("zjgsid");

        if (!StringUtils.hasLength(zjgsid)) {
            throw new SystemException("中介公司ID不存在");
        }

        Azjgs azjgs = zjgsService.getById(zjgsid);
        param.put("azjgs", azjgs);

        Azj azj = zjService.getById(azjgs.getFzzjid());
        param.put("azj", azj);

        QueryWrapper<Azj> zjqw = new QueryWrapper<>();
        zjqw.eq("zjgsid", azjgs.getZjgsid());
        List<Azj> azjs = zjService.list(zjqw);
        param.put("ywyCount", azjs.size());
        if (azjs != null && azjs.size() > 0) {
            JSONArray commissionids = new JSONArray();
            azjs.forEach(item -> {
                commissionids.add(item.getCommissionid());
            });

            QueryWrapper<Acommission> comqw = new QueryWrapper<>();
            comqw.in("commissionid", commissionids);
            List<Acommission> acommissions = commissionService.list(comqw);
            Integer total = acommissions.stream().mapToInt(comm -> {
                return comm.getTotal();
            }).sum();
            Acommission acommission = new Acommission();
            acommission.setTotal(total);
            param.put("acommission", acommission);
        }

        // 获取门店的佣金信息
        QueryWrapper<Acommissionitem> ciqw = new QueryWrapper<>();
        ciqw.eq("zjgsid", zjgsid);
        Long commissionCount = commissionitemService.count(ciqw);
        param.put("cCount", commissionCount);

        QueryWrapper<Astore> sqw = new QueryWrapper<>();
        sqw.eq("zjgsid", zjgsid);
        Long storeCount = astoreService.count(sqw);
        param.put("sCount", storeCount);

        QueryWrapper<Azjsqitem> zjsqqw = new QueryWrapper<>();
        zjsqqw.eq("zjgsid", zjgsid);
        Long zjgsidCount = zjsqitemService.count(zjsqqw);
        param.put("sqCount", zjgsidCount);

        return param;
    }

    /**
     * 新增中介申请
     * @param param
     * @return
     */
    public JSONObject newZjsq(JSONObject param) {

        Azjsqitem azjsqitem = param.getObject("sqitem", Azjsqitem.class);

        Azj azj = zjService.getById(azjsqitem.getZjid());
        Azjgs azjgs = zjgsService.getById(azjsqitem.getZjgsid());

        azjsqitem.setCt(LocalDateTime.now());
        azjsqitem.setGsname(azjgs.getZjgsnc());
        azjsqitem.setZjname(azj.getZjname());

        azjsqitem.setZjsqid(StringGenerator.getID("ZJSQ"));

        zjsqitemService.save(azjsqitem);

        param.put("azjsqitem", azjsqitem);
        return param;
    }

    /**
     * 取得所有中介申请
     * @param param
     * @return
     */
    public JSONObject getZjsqitems(JSONObject param) {
        QueryWrapper<Azjsqitem> sqqw = new QueryWrapper<>();
        String zjid = param.getString("zjid");
        if (StringUtils.hasLength(zjid)) {
            sqqw.eq("zjid", zjid);
        }

        String zjgsid = param.getString("zjgsid");
        if (StringUtils.hasLength(zjgsid)) {
            sqqw.eq("zjgsid", zjgsid);
        }

        List<Azjsqitem> sqitems = zjsqitemService.list(sqqw);
        param.put("zjsqitems", sqitems);
        return param;
    }

    /**
     * 删除中介申请
     * @param param
     * @return
     */
    @Transactional
    public JSONObject deleteZjsqitems(JSONObject param) {

        Azjsqitem item = param.getObject("item", Azjsqitem.class);
        String option = param.getString("option");

        if ("agree".equals(option)) {
            Azj azj = zjService.getById(item.getZjid());
            azj.setZjgsid(item.getZjgsid());
            zjService.updateById(azj);
            param.put("azj", azj);

            Azjgs azjgs = zjgsService.getById(item.getZjgsid());
            param.put("azjgs", azjgs);
        }

        zjsqitemService.removeById(item.getZjsqid());
        return param;
    }

    /**
     * 取得中介详细
     * @param param
     * @return
     */
    public JSONObject getZj(JSONObject param) {

        String zjid = param.getString("zjid");

        if (!StringUtils.hasLength(zjid)) {
            throw new SystemException("中介ID不存在");
        }

        Azj azj = zjService.getById(zjid);
        param.put("azj", azj);

        Acommission acommission = commissionService.getById(azj.getCommissionid());
        param.put("acommission", acommission);

        // 获取门店的佣金信息
        QueryWrapper<Acommissionitem> ciqw = new QueryWrapper<>();
        ciqw.eq("zjid", zjid);
        Long commissionCount = commissionitemService.count(ciqw);
        param.put("cCount", commissionCount);

        QueryWrapper<Astore> sqw = new QueryWrapper<>();
        sqw.eq("zjid", azj.getZjid());
        Long storeCount = astoreService.count(sqw);
        param.put("sCount", storeCount);

        return param;
    }

    /**
     * 取得中介业务员
     * @param param
     * @return
     */
    public JSONObject getZjs(JSONObject param) {

        QueryWrapper<Azj> zjqw = new QueryWrapper<>();
        String zjgsid = param.getString("zjgsid");
        zjqw.isNotNull("zjgsid");
        if (StringUtils.hasLength(zjgsid)) {
            zjqw.eq("zjgsid", zjgsid);
        }

        List<Azj> azjs = zjService.list(zjqw);
        param.put("azjs", azjs);
        return param;
    }

    /**
     * 中介业务员更新
     * @param param
     * @return
     */
    public JSONObject updateZj(JSONObject param) {
        Azj azj = param.getObject("azj", Azj.class);
        zjService.updateById(azj);
        return param;
    }

    public JSONObject getCommissionitems(JSONObject param) {

        String storeid = param.getString("storeid");

        QueryWrapper<Acommissionitem> ciqw = new QueryWrapper<>();

        if (StringUtils.hasLength(storeid)) {
            ciqw.eq("storeid", storeid);
        }

        List<Acommissionitem> commissions = commissionitemService.list(ciqw);
        param.put("commissions", commissions);
        return param;
    }

    public JSONObject getCommissionitemByPage(JSONObject param) {

        Integer current = param.getInteger("current");
        Integer size = param.getInteger("size");

        Page<Acommissionitem> page = new Page<>(current, size);
        QueryWrapper<Acommissionitem> qw = new QueryWrapper<>();
        String zjid = param.getString("zjid");
        if (StringUtils.hasLength(zjid)) {
            qw.eq("zjid", zjid);
        }

        String zjgsid = param.getString("zjgsid");
        if (StringUtils.hasLength(zjgsid)) {
            qw.eq("zjgsid", zjgsid);
        }

        List<JSONObject> yjPage = commissionitemMapper.getYjPage(page, qw);

        param.put("records", yjPage);
        param.put("total", page.getTotal());
        return param;
    }

    public JSONObject getCommissionitem(JSONObject param) {
        String itemid = param.getString("itemid");

        // 佣金item
        Acommissionitem acommissionitem = commissionitemService.getById(itemid);
        param.put("acommissionitem", acommissionitem);
        // 预约记录
        String rmorderid = acommissionitem.getRmorderid();
        Aroomyyjl aroomyyjl = roomyyjlService.getById(rmorderid);
        param.put("aroomyyjl", aroomyyjl);
        // 教练
        String jlid = aroomyyjl.getJlid();
        Ajiaolian ajiaolian = ajiaolianService.getOne(new QueryWrapper<Ajiaolian>().eq("jlid", jlid));
        param.put("ajiaolian", ajiaolian);
        // 门店
        String storeid = aroomyyjl.getStoreid();
        Astore astore = astoreService.getOne(new QueryWrapper<Astore>().eq("storeid", storeid));
        param.put("astore", astore);
        return param;
    }

}
